package net.huashitong.ssydt.provider.article.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huashitong.ssydt.article.entity.SsArticleArticle;
import net.huashitong.ssydt.article.entity.SsArticleArticleText;
import net.huashitong.ssydt.provider.article.service.SsArticleArticleService;
import net.huashitong.ssydt.provider.article.service.SsArticleArticleTextService;
import net.huashitong.ssydt.provider.comment.service.CommentService;
import net.huashitong.ssydt.provider.comment.vo.CommentVO;
import net.huashitong.ssydt.security.utils.UserUtils;
import net.huashitong.user.UserManager;
import net.huashitong.user.http.Result;
import net.huashitong.user.results.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.09.15
 */

@Api(value = "/api/article/", tags = {"文章模块"})
@Controller
@RequestMapping("/api/article/")
public class ApiWebPageController {

    private SsArticleArticleService ssArticleArticleService;

    private SsArticleArticleTextService ssArticleArticleTextService;

    private UserManager userManager;

    private CommentService commentService;

    @Autowired
    public ApiWebPageController(SsArticleArticleService ssArticleArticleService,
                                SsArticleArticleTextService ssArticleArticleTextService,
                                UserManager userManager,
                                CommentService commentService) {
        this.ssArticleArticleService = ssArticleArticleService;
        this.ssArticleArticleTextService = ssArticleArticleTextService;
        this.userManager = userManager;
        this.commentService = commentService;
    }


    @ApiOperation(value = "文章详情", notes = "文章详情", produces = MediaType.TEXT_HTML_VALUE)
    @GetMapping("/articles/{articleId}")
    public String getArticleById(
            @ApiParam(value = "文章id") @PathVariable("articleId") Long articleId,
            Model model) {

        String userId = UserUtils.getUserId();

        //文章信息
        SsArticleArticle ssArticleArticle = ssArticleArticleService.selectByPrimaryKey(articleId);
        if (ssArticleArticle == null) {
            return getPageView("noarticle");
        }
        ssArticleArticleService.updateClickNumberAndHotNumber(ssArticleArticle.getId());
        ssArticleArticle.setClickNumber(ssArticleArticle.getClickNumber() + 1);
        SsArticleArticleText ssArticleArticleText = ssArticleArticleTextService.selectByArticleId(ssArticleArticle.getId());

        //用户信息
        Result<User> rs = userManager.get(ssArticleArticle.getCreateUserId());
        CommentVO.User user = new CommentVO.User();
        if (rs.getCode().equals(Result.Status.SUCCESS)) {
            if (rs.getResults() != null) {
                BeanUtils.copyProperties(rs.getResults(), user);
            } else {
                user.setImageUrl("/dist/img/@2x/head.png");
                user.setNickname("华叔");
            }
        } else {
            user.setImageUrl("/dist/img/@2x/head.png");
            user.setNickname("华叔");
        }
        //相关文章
        List<SsArticleArticle> articleList = ssArticleArticleService.queryLatelyThreeByIdWithArticleTypeId(ssArticleArticle);
        //评论列表
        List<CommentVO> commentList = commentService.getArticleTopFiveHotCommonList(articleId, userId);

        //判断是否有测评
        long articleTypeId = ssArticleArticle.getArticleTypeId();
        boolean isEvaluation = articleTypeId == 3L || articleTypeId == 4L;

        model.addAttribute("article", ssArticleArticle);
        model.addAttribute("articleText", ssArticleArticleText);
        model.addAttribute("user", user);
        model.addAttribute("articleList", articleList);
        model.addAttribute("commentList", commentList);
        model.addAttribute("isEvaluation", isEvaluation);
        return getPageView("detail");
    }


    /**
     * 避坑指南，返回thymeleaf模板页面的时，开头不带"/"
     *
     * @param view 相对路径
     * @return html页面地址
     */
    private String getPageView(String view) {
        return String.format("api/article/%s", view);
    }


}
